/*-----------------------------------------------------
 * 权限子系统-系统反馈模块
 * ---------------------------------------------------*/
jx.complete(function () {
    //定义变量
    var api = {
        grid: '/auth/feedback/grid',
        details: '/auth/feedback/details',
        close: '/auth/feedback/close'
    };
    var $grid = $('#grid'),$gridform = $('#gridform'),
        $timeButton = $('#time-horizon > a'),
        $griddatePopover = $('#griddate-popover'),$gridPanel;
    var gridInstance;
    var detailsTitle = '查看系统发布记录';
    var gridUrl = jx.apiUrl(api.grid),detailsUrl = jx.apiUrl(api.details),
        closeUrl = jx.apiUrl(api.close);

    var formSubmit = function(){
        $gridform.find(':submit').click();
    }

    var setTimeHorizon = function ($this) {
        $timeButton.removeClass('active');
        $this.addClass('active');
        var val = $this.attr('data-value');
        if(val=='5') return;
        var $start = $('#operateStartDateTime');
        var $end = $('#operateEndDateTime');
        var cdate = jx.formatDate(new Date());
        switch (val) {
            case '1'://今天
                $start.val(cdate);
                $end.val(cdate);
                formSubmit();
                break;
            case '2'://近7天
                $start.val(jx.formatDate(jx.addDate(new Date(), -7)));
                $end.val(cdate);
                formSubmit();
                break;
            case '3'://近一个月
                $start.val(jx.formatDate(jx.addDate(new Date(), -30)));
                $end.val(cdate);
                formSubmit();
                break;
            case '4'://近三个月
                $start.val(jx.formatDate(jx.addDate(new Date(), -90)));
                $end.val(cdate);
                formSubmit();
                break;
            case '5'://自定义
                break;
        }
    };

    //初始化表格
    var initGrid = function () {
        gridInstance = $grid.jxgrid({
            url: gridUrl,
            onDblClickRow: function (index, row) {
                detailsData(row);
            },
            onBeforeLoad: function (param) {
                if (param.operateEndDateTime && param.operateStartDateTime) {
                    return true;
                }
                return false;
            }
        });
        $gridPanel = gridInstance.getPanel();
    };

    //初始化事件
    var initEvent = function () {
        // 表格事件
        $gridPanel.on('click', '.gridstatus', function () {
            var id = $(this).data('id');
            var val = $(this).data('val');
            closeFeedback(id, val);
        });
        $('#btn-datesearch').click(function () {
            if ($('#griddateform').valid()) {
                formSubmit();
                $griddatePopover.popoverX('hide');
            }
        });
        $timeButton.click(function () {
            setTimeHorizon($(this));
        });
    };

    //查看数据
    var detailsData = function (row) {
        jx.auth.detailsData(gridInstance, row, {
            title: detailsTitle,
            url: detailsUrl,
            offset:'auto',
            maxmin: true,
            width: '60%',
            height: '100%'
        });
    };

    //关闭反馈
    var closeFeedback = function (id, val) {
        if (val == '1') return false;
        jx.ajax({
            url: closeUrl,
            data: {id: id},
            confirm: '注：您确定要关闭此反馈吗？关闭后所有人都无法再进行回复',
            maskMsg: '正在关闭反馈...',
            success: function (result) {
                gridInstance.reloadGridData();
                jx.toastr.success('反馈关闭成功');
            }
        });
    };

    //定义表格列格式化函数
    jx.gf.status = function (v, row, index) {
        var cls;
        if ((jx.isNumber(v) && v === 1) || (jx.isBool(v) && v === true)) {
            cls = 'success';
        }
        else {
            cls = 'danger';
        }
        return '<span class="label label-' + cls + ' gridstatus" style="cursor: pointer" data-id="' + row.id + '" data-val="' + v + '">' + row.statusText + '</span>';
    };

    jx.gf.replyStatus = function (v, row, index) {
        var cls;
        if ((jx.isNumber(v) && v === 1) || (jx.isBool(v) && v === true)) {
            cls = 'success';
        }
        else {
            cls = 'danger';
        }
        return '<span class="label label-' + cls + '">' + row.replyStatusText + '</span>';
    };

    //对外接口
    //重新加载表格数据
    window.reloadGridData = function () {
        gridInstance.reloadGridData();
    };

    //初始化
    initGrid();
    initEvent();
    $timeButton.eq(3).click();
});